/*
 * File: PhotoFrameModel.cs
 * Author: Gourav Das
 * Purpose: Model for PhotoFrames
 * Created: May 19 2011
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using _22yards.Core;

namespace _22yards.ThreeD
{
    /// <summary>
    /// This is a game component that implements CustomModel.
    /// </summary>
    public class PhotoFrameModel : CustomModel
    {
        /// <summary>
        /// Occurs when Object is Mouse Left clicked Selected
        /// </summary>
        public event EventHandler MouseLeftClick;

        /// <summary>
        /// Occurs when [mouse right key selected].
        /// </summary>
        public event EventHandler MouseRightClick;

        /// <summary>
        /// Occurs when [mouse left double clicks].
        /// </summary>
        public event EventHandler MouseLeftDoubleClick;

        /// <summary>
        /// Gets or sets the player ID of the player for this Photoframe
        /// </summary>
        /// <value>
        /// The player ID.
        /// </value>
        public cPlayer PlayerID
        {
            get;
            set;
        }

        private int oscDir; //0 No Osc, 1 right Osc, 2 left Osc
        private float angle = 0;

        private float oscSpeed = 0.01f;

        /// <summary>
        /// Gets or sets the oscillation speed.
        /// </summary>
        /// <value>
        /// The oscillation speed.
        /// </value>
        public float OscillationSpeed
        {
            get
            {
                return oscSpeed;
            }
            set
            {
                oscSpeed = value;
            }
        }

        /// <summary>
        /// Resets the oscillation speed.
        /// </summary>
        public void ResetOscillationSpeed()
        {
            oscSpeed = 0.01f;
        }


        /// <summary>
        /// Gets the osc dir. 0 - No Oscillations, 1 - left, 2 - right
        /// </summary>
        public int OscDir
        {
            get
            {
                return oscDir;
            }
        }

        /// <summary>
        /// Gets the angle of oscillations
        /// </summary>
        public float Angle
        {
            get
            {
                return angle;
            }
        }

        private bool isMouseLeftClickSelected = false;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is selected.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is selected; otherwise, <c>false</c>.
        /// </value>
        public bool IsMouseLeftClickSelected
        {
            get
            {
                return isMouseLeftClickSelected;
            }
            set
            {
                isMouseLeftClickSelected = value;
                if (this.MouseLeftClick != null && isSelectable && isMouseLeftClickSelected ==true)
                    this.MouseLeftClick(this, new EventArgs());
            }
        }

        private bool isMouseRightClickSelected = false;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is selected.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is selected; otherwise, <c>false</c>.
        /// </value>
        public bool IsMouseRightClickSelected
        {
            get
            {
                return isMouseRightClickSelected;
            }
            set
            {
                isMouseRightClickSelected = value;
                if (this.MouseRightClick != null && isSelectable && isMouseRightClickSelected == true)
                    this.MouseRightClick(this, new EventArgs());
            }
        }

        private bool isMouseLeftDoubleClickSelected;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is mouse left double click selected.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is mouse left double click selected; otherwise, <c>false</c>.
        /// </value>
        public bool IsMouseLeftDoubleClickSelected
        {
            get
            {
                return isMouseLeftDoubleClickSelected;
            }
            set
            {
                isMouseLeftDoubleClickSelected = value;
                if (this.MouseLeftDoubleClick != null && isSelectable && isMouseLeftDoubleClickSelected == true)
                    this.MouseLeftDoubleClick(this, new EventArgs());
            }
        }

        private bool isSelectable = false;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is selectable.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is selectable; otherwise, <c>false</c>.
        /// </value>
        public bool IsSelectable
        {
            get
            {
                return isSelectable;
            }
            set
            {
                isSelectable = value;
            }
        }

        private bool oscillate = true;
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="PhotoFrameModel"/> will oscillate.
        /// </summary>
        /// <value>
        ///   <c>true</c> if oscillate; otherwise, <c>false</c>.
        /// </value>
        public bool Oscillate
        {
            get
            {
                return oscillate;
            }
            set
            {
                oscillate = value;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PhotoFrameModel"/> class.
        /// </summary>
        /// <param name="_Model">The _ model.</param>
        /// <param name="_Position">The _ position.</param>
        /// <param name="_Rotation">The _ rotation.</param>
        /// <param name="_Scale">The _ scale.</param>
        /// <param name="_graphicsDevice">The _graphics device.</param>
        /// <param name="_Name">Name of the _.</param>
        /// <param name="_ID">The _ ID.</param>
        /// <param name="Oscillate">The oscillate.</param>
        public PhotoFrameModel(Model _Model, Vector3 _Position, Vector3 _Rotation,
            Vector3 _Scale, GraphicsDevice _graphicsDevice, string _Name, short _ID, int Oscillate)
            : base(_Model,
            _Position,_Rotation,_Scale,ref _graphicsDevice,_Name,_ID)
        {
            oscDir = Oscillate;
            angle = (oscDir == 1) ? 0 : 360;
        }

        private void OscillatePhotoFrame()
        {
            Rotation = new Vector3(0, (float)Math.Sin(MathHelper.ToRadians(angle)) * 0.01f, 0);
            if (oscDir == 1)
            {
                if (angle > 360f)
                {
                    angle = 0;
                }
                else
                {
                    angle += 3;
                }
            }
            else
            {
                if (angle < 0f)
                    angle = 360f;
                else
                    angle -= 3;
            }
        }

        /// <summary>
        /// Syncs the oscillations with another model.
        /// </summary>
        /// <param name="_model">The _model.</param>
        public void SyncOscillationsWithModel(PhotoFrameModel _model)
        {
            oscDir = _model.OscDir;
            angle = _model.Angle;
        }

        /// <summary>
        /// Updates this instance.
        /// </summary>
        public override void Update()
        {
            if (oscDir > 0 && oscillate)
                OscillatePhotoFrame();

            base.Update();
        }

        ///// <summary>
        ///// Draws the specified camera.
        ///// </summary>
        ///// <param name="Camera">The camera.</param>
        ///// <param name="_ModelWorld">The _ model world.</param>
        //public override void Draw(BaseCamera Camera, Matrix _ModelWorld)
        //{
        //    Reset3DVariable();
        //    // Calculate the base transformation by combining
        //    // translation, rotation, and scaling
        //    Matrix baseWorld = Matrix.CreateScale(Scale)
        //    * Matrix.CreateFromYawPitchRoll(
        //    Rotation.Y, Rotation.X, Rotation.Z)
        //    * Matrix.CreateTranslation(Position);
        //    foreach (ModelMesh mesh in model.Meshes)
        //    {
        //        Matrix localWorld = modelTransforms[mesh.ParentBone.Index] * _ModelWorld * baseWorld;

        //        foreach (ModelMeshPart meshPart in mesh.MeshParts)
        //        {
        //            Effect effect = meshPart.Effect;

        //            if (effect is BasicEffect)
        //            {
        //                ((BasicEffect)effect).World = localWorld;
        //                ((BasicEffect)effect).View = Camera.ViewMatrix;
        //                ((BasicEffect)effect).Projection = Camera.ProjectionMatrix;
        //                ((BasicEffect)effect).EnableDefaultLighting();
        //            }
        //            else
        //            {
        //                setEffectParameter(effect, "World", localWorld);
        //                setEffectParameter(effect, "View", Camera.ViewMatrix);
        //                setEffectParameter(effect, "Projection", Camera.ProjectionMatrix);
        //                setEffectParameter(effect, "CameraPosition", Camera.Position);
        //            }
        //            MMaterial.SetEffectParameters(effect);
        //        }
        //        mesh.Draw();
        //    }
        //}
    }
        
}
